#region (c) 2014 THN Solutions LLC. - All rights reserved
/*
THN DATA ABSTRACTION LAYER version 1

Copyright (c) 2014, THN Solutions LLC. ( www.ThnSolutions.com )
Author: Nguyễn, M. Hải                 ( www.Minh-Hai.com     )
All rights reserved.

This library is dual-licensed.
  + For commercial software, please obtain a commercial license from THN Solutions LLC.

  + For free software, this library is licensed under GPL version 3. A summary of GPLv3 is
    listed below. You should also find a copy of GPLv3 as file License_GPLv3.txt included
	with the source-code files.

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License version 3 as published by
    the Free Software Foundation  of the License.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#endregion

#region Using directives
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using Thn.Data.Exceptions;
using Thn.Data.Settings;
using Thn.Application;
#endregion

namespace Thn.Data.Database.Sql.Dialect
{
    /// <summary>
    /// Dialect specifications for Microsoft SQL database
    /// </summary>
    public class MsSqlDialect : AbstractDialect
    {
        #region Create Connection
        /// <summary>
        /// Construct a database specific connection instance
        /// </summary>
        public override IDbConnection CreateConnection()
        { return new SqlConnection(); }
        #endregion

        #region Create command
        /// <summary>
        /// Construct a database specific command instance
        /// </summary>
        public override IDbCommand CreateCommand()
        { return new SqlCommand(); }
        #endregion

        #region Create Parameter
        /// <summary>
        /// Construct a database specific parameter instance
        /// </summary>
        /// <param name="paramName">Name of parameter</param>
        /// <param name="valueType">.NET type of parameter's value</param>
        public override IDataParameter CreateParameter(string paramName, Type valueType)
        {
            SqlParameter result = null;
            Hashtable typeMap = this.TypeMap;

            if (valueType.IsEnum) result = new SqlParameter(paramName, SqlDbType.Int);
            else if (valueType == typeof(string))
            {
                //string type is a tricky type. I'll simply let the Sql driver determine what type is best for me
                //instead of telling which SqlDbType to use
                result = new SqlParameter();
                //result.ParameterName = "@" + paramName;
                result.ParameterName = paramName;
            }
            else if (typeMap.Contains(valueType)) result = new SqlParameter(paramName, (SqlDbType)typeMap[valueType]);
            else if (!valueType.IsPrimitive && valueType.IsClass)
            {
                result = new SqlParameter(paramName, SqlDbType.Text);
            }
            else
            {
                //throw exception when this dialect doesn't support the provided value type
                TypeNotSupportedException.Publish(valueType);
            }

            return result;                
        }        
        #endregion

        #region Fixed Length String
        /// <summary>
        /// Returns the equivalent data type when a string attribute has specified size
        /// </summary>
        protected override object GetFixedLengthStringType()
        {
            return SqlDbType.NVarChar;
        }

        /// <summary>
        /// Returns the string version of equivalent data typep when a string attribute has specified size
        /// </summary>
        protected override string GetFixedLengthStringTypeAsString()
        {
            return "nvarchar";
        }
        #endregion

        #region Map Types
        /// <summary>
        /// Maps .NET types to data types.
        /// </summary>
        /// <param name="typeMap">key: .NET type, value: data type</param>
        /// <param name="stringMap">key: .NET type, value: data type as string</param>
        protected override void MapTypes(Hashtable typeMap, Hashtable stringMap)
        {
            typeMap.Add(typeof(Guid), SqlDbType.UniqueIdentifier);
            typeMap.Add(typeof(int), SqlDbType.Int);
            typeMap.Add(typeof(long), SqlDbType.BigInt); //int64
            typeMap.Add(typeof(short), SqlDbType.SmallInt); //int16
            typeMap.Add(typeof(bool), SqlDbType.Bit);
            typeMap.Add(typeof(DateTime), SqlDbType.DateTime);
            typeMap.Add(typeof(decimal), SqlDbType.Money);
            typeMap.Add(typeof(string), SqlDbType.NText);
            typeMap.Add(typeof(double), SqlDbType.Float);
            typeMap.Add(typeof(Single), SqlDbType.Real);
            
            stringMap.Add(typeof(Guid), "uniqueidentifier");
            stringMap.Add(typeof(int), "int");
            stringMap.Add(typeof(long), "bigint"); //int64
            stringMap.Add(typeof(short), "smallint"); //int16
            stringMap.Add(typeof(bool), "bit");
            stringMap.Add(typeof(DateTime), "datetime");
            stringMap.Add(typeof(decimal), "money");
            stringMap.Add(typeof(string), "ntext");
            stringMap.Add(typeof(double), "float");
            stringMap.Add(typeof(Single), "real");
        }
        #endregion

        #region Parse Exception
        /// <summary>
        /// Parse a database specific exception to standard one (known to this data layer)
        /// </summary>
        /// <param name="error">original exception</param>
        /// <returns>standardized exception</returns>
        protected override DataExceptionBase OnParseException(Exception error)
        {
            DataExceptionBase result = new DataExceptionBase(error);            

            return result;
        }
        #endregion

        #region Constructor
        /// <summary>
        /// Default constructor
        /// </summary>
        public MsSqlDialect()
        {
            this.mSupportMultipleCommands = true;            
        }
        #endregion
    }
}

